package sar

import org.springframework.dao.DataIntegrityViolationException

class TeachingSessionController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [teachingSessionInstanceList: TeachingSession.list(params), teachingSessionInstanceTotal: TeachingSession.count()]
    }

    def create() {
        [teachingSessionInstance: new TeachingSession(params)]
    }

    def save() {
       def teachingSessionInstance = new TeachingSession(params)
		def allStudentCourse=StudentCourse.findAllWhere(programCourse:teachingSessionInstance.programCourse);
		if (!teachingSessionInstance.save(flush: true)) {
			render(view: "create", model: [teachingSessionInstance: teachingSessionInstance])
			return
		}
		for(StudentCourse item in allStudentCourse){
			def studentInstant=new StudentAttendant(teachingSession:teachingSessionInstance,studentCourse:item,attendant:false)
			if(!studentInstant.save(failOnError:true)){
				println "Ko insert duoc"
			}
		}
		flash.message = message(code: 'default.created.message', args: [
			message(code: 'teachingSession.label', default: 'TeachingSession'),
			teachingSessionInstance.id
		])
		redirect(action: "show", id: teachingSessionInstance.id)
    }

    def show(Long id) {
        def teachingSessionInstance = TeachingSession.get(id)
        if (!teachingSessionInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'teachingSession.label', default: 'TeachingSession'), id])
            redirect(action: "list")
            return
        }

        [teachingSessionInstance: teachingSessionInstance]
    }

    def edit(Long id) {
        def teachingSessionInstance = TeachingSession.get(id)
        if (!teachingSessionInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'teachingSession.label', default: 'TeachingSession'), id])
            redirect(action: "list")
            return
        }

        [teachingSessionInstance: teachingSessionInstance]
    }

    def update(Long id, Long version) {
        def teachingSessionInstance = TeachingSession.get(id)
        if (!teachingSessionInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'teachingSession.label', default: 'TeachingSession'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (teachingSessionInstance.version > version) {
                teachingSessionInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'teachingSession.label', default: 'TeachingSession')] as Object[],
                          "Another user has updated this TeachingSession while you were editing")
                render(view: "edit", model: [teachingSessionInstance: teachingSessionInstance])
                return
            }
        }

        teachingSessionInstance.properties = params

        if (!teachingSessionInstance.save(flush: true)) {
            render(view: "edit", model: [teachingSessionInstance: teachingSessionInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'teachingSession.label', default: 'TeachingSession'), teachingSessionInstance.id])
        redirect(action: "show", id: teachingSessionInstance.id)
    }

    def delete(Long id) {
        def teachingSessionInstance = TeachingSession.get(id)
        if (!teachingSessionInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'teachingSession.label', default: 'TeachingSession'), id])
            redirect(action: "list")
            return
        }

        try {
            teachingSessionInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'teachingSession.label', default: 'TeachingSession'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'teachingSession.label', default: 'TeachingSession'), id])
            redirect(action: "show", id: id)
        }
    }
}
